package com.electronic;

import com.alibaba.fastjson.JSON;
import com.cebpubservice.cashare.sdk.electronicsealsignatureandverifysignature.result.BatchGetCAOrgCertChainInfoResultDTO;
import com.electronic.api.R;
import com.electronic.domain.CebsSdkProperties;
import com.electronic.modules.electronicSealSignature.ElectronicSealSignatureService;
import com.electronic.modules.electronicSealSignature.ExecuteSignatureService;
import com.electronic.modules.electronicSealSignature.param.CebsSignatureDTO;
import com.electronic.modules.electronicSealSignature.param.ExitElectronicSealSignatureDTO;
import com.electronic.modules.electronicSealSignature.param.StartSignDTO;
import com.electronic.modules.electronicSealSignature.result.*;
import com.electronic.modules.other.ComputerInfoService;
import com.electronic.modules.other.DirService;
import com.electronic.modules.other.ReceiveAndSaveSignedFileHashService;
import com.electronic.modules.other.param.CopyDirDTO;
import com.electronic.modules.other.param.CreateDirDTO;
import com.electronic.modules.other.param.ReceiveAndSaveSignedFilePathDTO;
import com.electronic.modules.other.result.ComputerInfo;
import com.electronic.modules.other.result.ReceiveAndSaveSignedFileHashVO;
import com.electronic.modules.revoke.RevokeService;
import com.electronic.modules.revoke.param.GenerateRevokeSignatureQRCodeDTO;
import com.electronic.modules.revoke.param.QRCodeScannedStatusDTO;
import com.electronic.modules.revoke.param.RevokeSignatureDTO;
import com.electronic.modules.revoke.result.GenerateRevokeSignatureQRCodeVO;
import com.electronic.modules.revoke.result.QRCodeScannedStatusVO;
import com.electronic.modules.revoke.result.RevokeResultVO;
import com.electronic.modules.verify.CertChainService;
import com.electronic.modules.verify.VerifyService;
import com.electronic.modules.verify.param.*;
import com.electronic.modules.verify.result.GetFileElectronicSealSignatureListVO;
import com.electronic.modules.verify.result.GetOneVerifyResultVO;
import com.electronic.modules.verify.result.GetVerifyResultVO;
import lombok.extern.slf4j.Slf4j;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;


@Slf4j
public class Application {
    public static void main(String[] args) {
        log.info("===jar包被调用===");
        // 检查参数是否足够
        if (args.length < 2) {
            log.error("参数不足");
            return;
        }
        // 获取命令行参数
        String name = args[0];
        String data = args[1];
        Map<String, Object> map = new ConcurrentHashMap<>();
        // 调用方法并传递参数
        switch (name) {

            case "SignatureQrCode":
                // 获取二维码
                CebsSdkProperties properties = JSON.parseObject(data, CebsSdkProperties.class);
                R<GetSignQrCodeVO> signatureQrCode = ElectronicSealSignatureService.getSignatureQrCode(properties);
                map.put("SignatureQrCode", signatureQrCode);
                log.info(JSON.toJSONString(map));
                break;
            case "QrCodeScanStatus":
                // 获取二维码状态
                QrCodeScanDTO qrCodeScanDTO = JSON.parseObject(data, QrCodeScanDTO.class);
                GetQrCodeScanStatusVO qrCodeScanStatus = ElectronicSealSignatureService.getQrCodeScanStatus(qrCodeScanDTO);
                map.put("QrCodeScanStatus", R.data(qrCodeScanStatus));
                log.info(JSON.toJSONString(map));
                break;
            case "UserSeals":
                //获取签章列表
                CebsSignatureDTO cebsSignatureDTO = JSON.parseObject(data, CebsSignatureDTO.class);
                List<GetUserSealsVO> userSeals = ElectronicSealSignatureService.getUserSeals(cebsSignatureDTO);
                map.put("UserSeals", R.data(userSeals));
                log.info(JSON.toJSONString(map));
                break;
            case "CommitSign":
                //提交签章
                StartSignDTO startSignDTO = JSON.parseObject(data, StartSignDTO.class);
                GetSignResultVO getSignResultVO = ElectronicSealSignatureService.commitSign(startSignDTO);
                map.put("CommitSign", R.data(getSignResultVO));
                log.info(JSON.toJSONString(map));
                break;
            case "GenerateRevokeSignatureQRCode":
                //生成撤章二维码
                GenerateRevokeSignatureQRCodeDTO generateRevokeSignatureQRCodeDTO = JSON.parseObject(data, GenerateRevokeSignatureQRCodeDTO.class);
                GenerateRevokeSignatureQRCodeVO generateRevokeSignatureQRCodeVO = RevokeService.generateRevokeSignatureQRCode(generateRevokeSignatureQRCodeDTO);
                map.put("GenerateRevokeSignatureQRCode", R.data(generateRevokeSignatureQRCodeVO));
                log.info(JSON.toJSONString(map));
                break;
            case "GetQRCodeScannedStatus":
                //获取撤章二维码扫码状态
                QRCodeScannedStatusDTO qrCodeScannedStatusDTO = JSON.parseObject(data, QRCodeScannedStatusDTO.class);
                QRCodeScannedStatusVO qrCodeScannedStatusVO = RevokeService.getQRCodeScannedStatus(qrCodeScannedStatusDTO);
                map.put("GetQRCodeScannedStatus", R.data(qrCodeScannedStatusVO));
                log.info(JSON.toJSONString(map));
                break;
            case "Revoke":
                //撤章
                RevokeSignatureDTO revokeSignatureDTO = JSON.parseObject(data, RevokeSignatureDTO.class);
                RevokeResultVO revokeResultVO = RevokeService.revoke(revokeSignatureDTO);
                map.put("Revoke", R.data(revokeResultVO));
                log.info(JSON.toJSONString(map));
                break;
            case "CommitVerifySignature":
                //获取验章结果
                CommitVerifySignatureDTO commitVerifySignatureDTO = JSON.parseObject(data, CommitVerifySignatureDTO.class);
                GetVerifyResultVO getVerifyResultVO = VerifyService.commitVerifySignature(commitVerifySignatureDTO);
                map.put("CommitVerifySignature", R.data(getVerifyResultVO));
                log.info(JSON.toJSONString(map));
                break;
            case "GetSignatureInfo":
                //获取文件签章信息
                GetSignatureInfoDTO getSignatureInfoDTO = JSON.parseObject(data, GetSignatureInfoDTO.class);
                GetOneVerifyResultVO getSignatureInfo = VerifyService.getSignatureInfo(getSignatureInfoDTO);
                map.put("GetSignatureInfo", R.data(getSignatureInfo));
                log.info(JSON.toJSONString(map));
                break;
            case "GetSignaturesInfo":
                //获取文件签章信息
                GetSignatureListInfoDTO getSignatureListInfoDTO = JSON.parseObject(data, GetSignatureListInfoDTO.class);
                List<GetOneVerifyResultVO> signaturesInfo = VerifyService.getSignaturesInfo(getSignatureListInfoDTO);
                map.put("GetSignaturesInfo", R.data(signaturesInfo));
                log.info(JSON.toJSONString(map));
                break;
            case "GetCertChain":
                //获取证书链
                CebsSdkProperties cebsSdkProperties = JSON.parseObject(data, CebsSdkProperties.class);
                List<BatchGetCAOrgCertChainInfoResultDTO> batchGetCAOrgCertChainInfoResultDTOList = CertChainService.getCertChain(cebsSdkProperties).getData();
                map.put("GetCertChain", CertChainService.getCertChain(cebsSdkProperties));
                log.info(JSON.toJSONString(map));
                break;
            case "CreateDir":
                //创建文件
                CreateDirDTO createDirDTO = JSON.parseObject(data, CreateDirDTO.class);
                map.put("CreateDir", DirService.createDir(createDirDTO));
                log.info(JSON.toJSONString(map));
                break;
            case "SaveDir":
                //签署完成
                CopyDirDTO copyDirDTO = JSON.parseObject(data, CopyDirDTO.class);
                map.put("SaveDir", DirService.copyDir(copyDirDTO));
                log.info(JSON.toJSONString(map));
                break;
            case "DownloadFile":
                //下载文件
                DownloadFileDTO downloadFileDTO = JSON.parseObject(data, DownloadFileDTO.class);
                map.put("DownloadFile", DirService.downloadFile(downloadFileDTO));
                log.info(JSON.toJSONString(map));
                break;
            case "CheckAdminPermission":
                //获取文件夹权限
                CheckAdminPermissionDTO checkAdminPermissionDTO = JSON.parseObject(data, CheckAdminPermissionDTO.class);
                map.put("CheckAdminPermission", DirService.CheckAdminPermission(checkAdminPermissionDTO));
                log.info(JSON.toJSONString(map));
                break;
            case "ExitElectronicSealSignature":
                //退出连续签章
                ExitElectronicSealSignatureDTO exitElectronicSealSignatureDTO = JSON.parseObject(data, ExitElectronicSealSignatureDTO.class);
                boolean b = ExecuteSignatureService.exitElectronicSealSignature(exitElectronicSealSignatureDTO);
                map.put("ExitElectronicSealSignature", R.data(b));
                log.info(JSON.toJSONString(map));
                break;
            case "GetFileElectronicSealSignatureList":
                //验章模块-获取文件签章列表
                GetFileElectronicSealSignatureListDTO getFileElectronicSealSignatureListDTO = JSON.parseObject(data, GetFileElectronicSealSignatureListDTO.class);
                List<GetFileElectronicSealSignatureListVO> fileElectronicSealSignatureList = VerifyService.getFileElectronicSealSignatureList(getFileElectronicSealSignatureListDTO);
                map.put("GetFileElectronicSealSignatureList", R.data(fileElectronicSealSignatureList));
                log.info(JSON.toJSONString(map));
                break;
            case "ComputerInfo":
                //获取系统配置信息
                R<ComputerInfo> computerInfo = ComputerInfoService.getComputerInfo();
                map.put("ComputerInfo", R.data(computerInfo));
                log.info(JSON.toJSONString(map));
                break;
            case "ReceiveAndSaveSignedFileHash":
                ReceiveAndSaveSignedFilePathDTO fileHashPath = JSON.parseObject(data, ReceiveAndSaveSignedFilePathDTO.class);
                //获取文件哈希
                R<ReceiveAndSaveSignedFileHashVO> receiveAndSaveSignedFileHash = ReceiveAndSaveSignedFileHashService.getFileHash(fileHashPath);
                map.put("ReceiveAndSaveSignedFileHash", R.data(receiveAndSaveSignedFileHash));
                log.info(JSON.toJSONString(map));
                break;
            default:
                log.info("没有找到对应的方法");
                break;
        }
    }
}
